package com.sihan.framework.etm.common;

import com.framework.mybatis.plugin.autofill.AutoFillPlugin;
import com.framework.mybatis.plugin.autofill.UsernameSource;
import com.framework.util.WebUtils;

import lombok.Data;

import java.math.BigDecimal;
import java.util.Locale;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
@Data
public class AppConfiguration {
	//图片根目录
	@Value("${pic.root}")
	private String rootPath;
	//图片格式化路径
	@Value("${pic.picFormatHost}")
	private String picFormatPath;
	//通信接口地址
	@Value("${communication.api.url}")
	private String communicationApiUrl;
	//验证是否人脸接口
	@Value("${communication.api.face-validate}")
	private String communicationFaceValidate;
	//图片比较接口
	@Value("${communication.api.face-compare}")
	private String communicationFaceCompare;
	//发送命令的api
	@Value("${communication.api.send-command}")
	private String sendCommandApi;
	//发送命令之后，等待超时时间，过时算失败
	@Value("${communication.api.wait-seconds}")
	private Integer waitSeconds;
	//等待设备交互的时间
	@Value("${communication.api.wait-operator}")
	private Integer waitOperatorSeconds;
	//最小正常温度
	@Value("${device-state.min-temperature}")
	private BigDecimal deviceMinTemperature;
	//最大正常温度
	@Value("${device-state.max-temperature}")
	private BigDecimal deviceMaxTemperature;
	//最小正常湿度
	@Value("${device-state.min-humidity}")
	private BigDecimal deviceMinHumidity;
	//最大正常湿度
	@Value("${device-state.max-humidity}")
	private BigDecimal deviceMaxHumidity;
	//发送打印
	@Value("${communication.api.send-command-print}")
	private String sendCommandPrint;
	//发送开始扫描
	@Value("${communication.api.send-command-ivg-scan}")
	private String sendCommandIvgScan;
	//发送开始扫描
	@Value("${communication.api.send-command-ivg-stop-scan}")
	private String sendCommandIvgStopScan;
	//设置编号
	@Value("${communication.api.send-command-ivg-set-code}")
	private String sendCommandIvgSet;
	//设置打印机开始打印
	@Value("${communication.api.send-print-start}")
	private String sendPrintStart;
	//发送打印内容
	@Value("${communication.api.send-print-content}")
	private String sendPrintContent;
	//关闭打印机
	@Value("${communication.api.send-print-close}")
	private String sendPrintClose;
	//柜子亮灯编号
	@Value("${device-light-num}")
	private Integer deviceLightNum;
	//开启读写icCard
	@Value("${communication.api.send-ic-card-register}")
	private String icCardStart;
	//关闭读取iccard
	@Value("${communication.api.send-ic-card-unRegister}")
	private String icCardEnd;

	@Value("${tool.default.name}")
	private String toolDefaultName;
	//图片x坐标的个数
	@Value("${coor-gris-x-lenght}")
	private int grisXlenght;
	//图片y坐标的个数
	@Value("${coor-gris-y-lenght}")
	private int grisYLenght;

	//图片4个角落的坐标从0开始的排序
	@Value("${coor-gris-corner-seri}")
	private String grisCornerSeri;
	//扫描识别任务
	@Value("${communication.api.send-operator-task-scan}")
	private String sendOperateTaskScan;
	//是被操作之后是否开门
	@Value("${distinguish.openDoor}")
	private Boolean distinguishOpenDoor;
	//mqtt服务器地址
	@Value("${mqtt-server.tcp}")
	private String mqttTcp;
	//mqtt用户名
	@Value("${mqtt-server.username}")
	private String mqttUsername;
	//mqtt密码
	@Value("${mqtt-server.password}")
	private String mqttPassword;
	//设备读标签的速度
	@Value("${device-read-speed}")
	private Integer readSpeed;
	//定位装置总电量
	@Value("${voltage-total}")
	private BigDecimal voltageTotal;

	//rtk总电压
	@Value("${voltage-total-RTK}")
	private BigDecimal voltageTotalRTK;

	//定位装置总上传次数
	@Value("${voltage-count}")
	private Integer voltageCount;

	//定位装置最大待机天数
	@Value("${voltage-wait-days}")
	private Integer voltageWaitDays;

	//柜子显示图片地址
	@Value("${device-img-addr}")
	private String deviceImgAddr;

	//#电压监测警示牌电压范围
	@Value("${jinshipai-voltage-range}")
	private String jinshipaiVoltageRange;


	@Bean
	public Realm realm() {
		ShiroRealm realm = new ShiroRealm();
		// realm.setName("customRealm");
		// realm.setAuthorizationCacheName("authorizationCache");
		return realm;
	}

	@Bean
	public Interceptor autoFillPlugin() {
		return new AutoFillPlugin(new UsernameSource() {
			@Override
			public String getUsername() {
				return WebUtils.username();
			}
		});
	}

	/**
	 * 国际化，设置默认的语言为中文 将用户的区域信息存在session 也可以存在cookie,由客户端保存
	 * {@link org.springframework.web.servlet.i18n.CookieLocaleResolver}
	 *
	 * @return
	 */
	@Bean
	public LocaleResolver localeResolver() {
		SessionLocaleResolver slr = new SessionLocaleResolver();
		slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
		return slr;
	}

	/**
	 * 国际化，设置url识别参数
	 *
	 * @return
	 */
	@Bean
	public LocaleChangeInterceptor localeChangeInterceptor() {
		LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
		lci.setParamName("lang");
		return lci;
	}

}
